package com.lys.controller;

import com.lys.entity.Order;
import com.lys.entity.OrderDetail;
import com.lys.service.OrderService;
import com.lys.utils.EmptyUtils;
import com.lys.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 陆小根
 * @version 9.0.4
 * @Date 2022/5/30 8:29
 */

@Controller
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单的主页面
     * @param request
     * @return
     */
    @GetMapping("/index")
    public String index(HttpServletRequest request) {
        //获取用户id
        String userId=request.getParameter("userId");
        //获取当前页数
        String currentPageStr = request.getParameter("currentPage");
        //获取页大小
        String pageSize = request.getParameter("pageSize");
        int rowPerPage  = EmptyUtils.isEmpty(pageSize)?5:Integer.parseInt(pageSize);
        int currentPage = EmptyUtils.isEmpty(currentPageStr)?1:Integer.parseInt(currentPageStr);
        int total=orderService.count(Integer.valueOf(userId));
        Pager pager=new Pager(total,rowPerPage,currentPage);
        pager.setUrl("admin/order/index?userId="+userId);
        List<Order> orderList=orderService.getOrderList(Integer.valueOf(userId), currentPage, rowPerPage);
        request.setAttribute("orderList", orderList);
        request.setAttribute("pager", pager);
        request.setAttribute("menu", 1);
        return "/backend/order/orderList";
    }
    /**
     * 查询订单明细
     * @return
     */
    @GetMapping("/{id}")
    public String queryOrderDeatil(@PathVariable("id") String id,
                                   Model model){
        //String orderId=request.getParameter("orderId");
        List<OrderDetail> orderDetailList=orderService.getOrderDetailList(Integer.valueOf(id));
        model.addAttribute("orderDetailList", orderDetailList);
        model.addAttribute("menu", 1);
        return "/backend/order/orderDetailList";
    }

    @GetMapping("/list")
    public String queryAllOrder(HttpServletRequest request){
        //获取当前页数
        String currentPageStr = request.getParameter("currentPage");
        //获取页大小
        String pageSize = request.getParameter("pageSize");
        int rowPerPage  = EmptyUtils.isEmpty(pageSize)?5:Integer.parseInt(pageSize);
        int currentPage = EmptyUtils.isEmpty(currentPageStr)?1:Integer.parseInt(currentPageStr);
        int total=orderService.count(null);
        Pager pager=new Pager(total,rowPerPage,currentPage);
        pager.setUrl("admin/order/list");
        List<Order> orderList=orderService.getOrderList(null, currentPage, rowPerPage);
        request.setAttribute("orderList", orderList);
        request.setAttribute("pager", pager);
        request.setAttribute("menu", 9);
        return "/backend/order/orderList";
    }

}
